package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.test.SurfaceActivity;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public RecyclerView recyclerView;
    private List<String> datas;
    private Button btn;
    private MyAdpapter myAdpapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datas = new ArrayList<>();
        for (int i = 0;i<40;i++) {
            datas.add(i+"");
        }
        btn = findViewById(R.id.btn);

        int[] arr = new int[]{4,1,8,5,3,2,9,10,6,7};
//        quickSort(arr,0,9);
//        new TestMethod().heapSort(arr);
        for (int i = 0; i < arr.length; i++) {
            Log.i("quickSort",arr[i]+",");
        }


        btn.setOnClickListener(v -> {

//
//            try {
//                Class classMethod = Class.forName("com.example.myapplication4.TestMethod");
//                Object method = classMethod.getConstructor(String.class, Context.class).newInstance("测试咯",MainActivity.this);
//
//               Method getTestStr =  classMethod.getDeclaredMethod("getTestStr");
//                getTestStr.setAccessible(true);
//                Object result = getTestStr.invoke(method);
//                Toast.makeText(MainActivity.this,result+"",Toast.LENGTH_LONG).show();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }


//            Toast.makeText(MainActivity.this,"当前点击事件",Toast.LENGTH_LONG).show();

//               String ster =  datas.get(2);
//               ster = "10";
//               datas.set(2,"10");
//                Toast.makeText(MainActivity.this,"ceshi ", Toast.LENGTH_LONG).show();
//                myAdpapter.notifyItemChanged(2);
//
                startActivity(new Intent(MainActivity.this, SurfaceActivity.class));
            //创建N个对象

        });
//        HookSetOnClickListenerHelper.hook(this,btn);

        myAdpapter = new MyAdpapter(this,datas);
        recyclerView = findViewById(R.id.recyclerv);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false);
//        layoutManager.set
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(myAdpapter);

    }


    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        if (arr != null && arr.length > 0) {
            int start = startIndex, end = endIndex;
            //target是本次循环要排序的元素，每次循环都是确定一个元素的排序位置，这个元素都是开始下标对应的元素
            int target = arr[startIndex];
            //开始循环，从两头往中间循环，相遇后循环结束
            while (start < end) {
                //从右向左循环比较，如果比target小，就和target交换位置，让所有比target小的元素到target的左边去
                while (start < end) {
                    if (arr[end] < target) {
                        swap(arr, start, end);
                        break;
                    } else {
                        end--;
                    }
                }

                //从左向右循环比较，如果比target大，就和target交换位置，让所有比target大的元素到target的右边去
                while (start < end) {
                    if (arr[start] > target) {
                        swap(arr, start, end);
                        break;
                    } else {
                        start++;
                    }
                }
            }
            //确定target的排序后，如果target左边还有元素，继续递归排序
            if ((start - 1) > startIndex) {
                quickSort(arr, startIndex, start - 1);
                for (int i = 0; i < arr.length; i++) {
                    Log.i("quickSort:排序左边",arr[i]+",");
                }
            }
            //确定target的排序后，如果target右边还有元素，继续递归排序
            if ((end + 1) < endIndex) {
                quickSort(arr, end + 1, endIndex);
            }
        }
    }
    public static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

}
